﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Vit.Common.Extensions;
using Vit.Caching;
using Vit.Web.Models;
using Microsoft.Practices.Unity;

namespace Vit.Web.Services
{
    public class CommonServices
    {
        public static T GetService<T>(string name)
        {
            IUnityContainer unity = ((VitApplication)HttpContext.Current.ApplicationInstance).UnityContainer;
            object service = unity.Resolve(typeof(T), name);
            if (service != null)
                return (T)service;
            return default(T);
        }

        public static IEnumerable<T> GetServices<T>()
        {
            IUnityContainer unity = ((VitApplication)HttpContext.Current.ApplicationInstance).UnityContainer;
            IEnumerable<object> services = unity.ResolveAll(typeof(T));
            if (services != null)
            {
                return services.Cast<T>();
            }
            return null;
        }

        public static IPluginService PluginService
        {
            get
            {
                return GetService<IPluginService>(null);
            }
        }

        public static ISiteService SiteService
        {
            get
            {
                return GetService<ISiteService>(null);
            }
        }

        public static ILocalizationService LocalizationService
        {
            get
            {
                return GetService<ILocalizationService>(null);
            }
        }

        public static IThemeService ThemeService
        {
            get
            {
                return GetService<IThemeService>(null);
            }
        }

        public static IValidatorService ValidatorService
        {
            get
            {
                return GetService<IValidatorService>(null);
            }
        }

        public static ILogService LogService
        {
            get
            {
                return GetService<ILogService>(null);
            }
        }

        public static ICacheManager CacheManager
        {
            get
            {
                return GetService<ICacheManager>(null);
            }
        }

        public static IAccountService AccountService
        {
            get
            {
                return GetService<IAccountService>(null);
            }
        }

        public static WebSite CurrentSite
        {
            get
            {

                IUnityContainer unity = ((VitApplication)HttpContext.Current.ApplicationInstance).UnityContainer;
                WebSite site = HttpContext.Current.Application["site"] as WebSite;
                if(site == null)
                   site = unity.Resolve(typeof(WebSite), null) as WebSite;
               if (site == null)
                   site = SiteService.GetCurrentSite();
               return site;
            }
        }
    }
}
